package com.ruoyi.word.service;

import com.ruoyi.common.utils.StringUtils;
import net.didion.jwnl.JWNL;
import net.didion.jwnl.JWNLException;
import net.didion.jwnl.data.IndexWord;
import net.didion.jwnl.data.POS;
import net.didion.jwnl.data.Synset;
import net.didion.jwnl.data.Word;
import net.didion.jwnl.dictionary.Dictionary;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class WordNetService {

    private Dictionary dictionary;

    public WordNetService() {
        try {
            InputStream is = new FileInputStream("/usr/local/wordnet/jwnl_properties.xml");
            JWNL.initialize(is);
            dictionary = Dictionary.getInstance();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Map getDefinitions(String word) {
        Map res =  new LinkedHashMap();
        if(dictionary == null) {
            return res;
        }
        try {
            List allPOS = POS.getAllPOS();
            allPOS.forEach(p -> {
                List<Map> data = new ArrayList<>();
                IndexWord indexWord = null;
                try {
                    indexWord = dictionary.getIndexWord((POS) p, word);
                    if (indexWord != null) {
                        Synset[] sens = indexWord.getSenses();
                        for (Synset synset : sens) {
                            List<String> ws = new LinkedList<>();
                            for (Word w1 : synset.getWords()) {
                                if(!StringUtils.equals(w1.getLemma(), word)) {
                                    ws.add(w1.getLemma());
                                }
                            }
                            Map m = new HashMap();
                            m.put("ws", ws);
                            String gloss = synset.getWord(0).getSynset().getGloss();
                            int pos = gloss.indexOf("; \"");
                            if(pos > 0) {
                                m.put("ss", gloss.substring(0, pos));
                                if (gloss.length() > pos + 2) {
                                    m.put("st", Arrays.asList(gloss.substring(pos + 2).split("; ")));
                                }
                            } else {
                                m.put("ss", gloss);
                            }
                            data.add(m);
                        }
                    }
                    if(!data.isEmpty()) {
                        res.put(((POS) p).getLabel().replace("noun", "n.").replace("verb", "v.").replace("adjective", "adj.").replace("adverb", "adv."), data);
                    }
                } catch (JWNLException e) {
                    throw new RuntimeException(e);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    public static void main(String[] args) throws JWNLException, IOException {
        WordNetService wordNet = new WordNetService();
        String word = "like"; // 要查询的单词
        System.out.println(wordNet.getDefinitions(word));
    }
}
